<template>
	<view class="userBindGroup">
		<view class="groupList flex justConBetween">
			<view class="leftBox">
				<template v-for="(item,index) in groupData">
					<view class="groupOne" :key="index" v-if="index%2==0">
						<uni-mygroup :groupItem="item" :canAdd="false" :canStar="false" :canDetail="false" @groupComOper="groupComOper"></uni-mygroup>
					</view>
				</template>
			</view>
			<view class="rightBox">
				<template v-for="(item,index) in groupData">
					<view class="groupOne" :key="index" v-if="index%2!=0">
						<uni-mygroup :groupItem="item" :canAdd="false" :canStar="false" :canDetail="false" @groupComOper="groupComOper"></uni-mygroup>
					</view>
				</template>
			</view>
		</view>
		<uni-nodata v-if="groupData.length==0"></uni-nodata>
		<view class="fixedBottomBtn" @click="toAdd">确定</view>
	</view>
</template>

<script>
	import { activityGroup,joinActivity } from "@/api/send.js"
	import { payBeforeTip } from "@/utils/common.js"
	export default {
		data() {
			return {
				groupData:[],
				page:1,
				pageSize:20,
				pageTotal:0,
				activityItem:'',
				chooseGroup:[]
			};
		},
		onLoad(option) {
			this.activityItem=option.activity?JSON.parse(option.activity):''
			this.getActAllGroup()
		},
		methods:{
			initPage(){
				this.page=1;
				this.groupData=[]
			},
			// 小组关联的列表
			getActAllGroup(){
				let param={
					page:this.page,
					size:this.pageSize,
					teamActivityId:this.activityItem.id
				}
				activityGroup(param).then((res)=>{
					if(res.code==100000){
						res.data.rows.forEach((item)=>{
							item.chooseStatus=false
						})
						this.groupData=this.groupData.concat(res.data.rows)
						this.pageTotal=Math.ceil(res.data.total/this.pageSize)
					}
				})
			},
			groupComOper(obj){
				if(obj.type=='add'){
					this.chooseGroup.push(obj.item)
				}else if(obj.type=='reduce'){
					this.chooseGroup.forEach((val,index)=>{
						console.log('循环查找',val,index,val.id,obj.item.id)
						if(val.id==obj.item.id){
							this.chooseGroup.splice(index,1)
							return
						}
					})
				}
				console.log("选择的小组===",this.chooseGroup,obj)
			},
			// 确定
			toAdd(){
				console.error("确定选择的小组",this.chooseGroup)
				// let chooseGroup=this.groupData.filter((item)=>item.chooseStatus)
				if(this.chooseGroup.length==0){
					uni.showToast({
						title:'请先选择要加入的小组',
						icon:'none'
					})
					return
				}
				if(this.chooseGroup.length>1){
					uni.showToast({
						title:'只能选择一个小组加入',
						icon:'none'
					})
					return
				}
				// console.log('要加入的小组',chooseGroup)
				if(this.activityItem.charge==1){
					let tip=`加入该活动需要付费${this.activityItem.amount}元，是否确定加入？`
					let param={
						type:4,
						money:this.activityItem.amount,
						id:this.activityItem.id,
						groupId:this.chooseGroup[0].id
					}
					payBeforeTip(param,tip)
					return
				}else{
					uni.showModal({
						title: '提示',
						content: '加入活动后需要按照活动设置的打卡模式，对任务进行打卡，是否确定加入？',
						success:(res)=>{
							if (res.confirm) {
								let param={
									teamId:this.chooseGroup[0].id,
									activityId:this.activityItem.id,
								}
								joinActivity(param).then((res)=>{
									if(res.code==100000){
										uni.showToast({
											title:'加入成功',
											icon:'none'
										})
										uni.navigateBack()
										// this.$emit('actComOper',{type:type,item:this.activityItem})
									}
								})
							} else if (res.cancel) {
							}
						}
					});
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
.groupList{
	flex-wrap: wrap;
	padding: 30rpx;
	box-sizing: border-box;
	&>view{
		flex: 0 0 48%;
	}
	.groupOne{
		margin-bottom: 30rpx;
		border-radius:8rpx;
	}
}
</style>
